package com.itheima.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //判断当前点的菜,购物车中是否存在
        //select * from shoppingCat where (dish_id =? 或者setmeal_id =)and user_id =?
        LambdaQueryWrapper<ShoppingCart> qw =new LambdaQueryWrapper<>();
        //判断是套餐id还是菜品id
        if (shoppingCart.getSetmealId()!=null){
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }else {
            qw.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }
        qw.eq(ShoppingCart::getUserId,UserHolder.get());
        ShoppingCart one = shoppingCartService.getOne(qw);
        if (one==null){ //证明不存在,没有点过,新增
            shoppingCart.setNumber(1);
            Long currentId = UserHolder.get();
            shoppingCart.setUserId(currentId);
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }else {
            //证明存在,并更新
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
    }
    /**
     * 查询购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //select *from shopping_cart where user_id =?
        LambdaQueryWrapper<ShoppingCart> qw =new LambdaQueryWrapper<>();
        qw.eq(ShoppingCart::getUserId, UserHolder.get());
        List<ShoppingCart> list = shoppingCartService.list(qw);
        return R.success(list);
    }

    /**
     * 取消购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        //菜品或者套餐id 查询出当前ShoppingCart数据
        // 如何是菜品：select * from shopping_cart where dish_id = ? and user_id = ?
        // 如果是套餐：select * from shopping_cart where setmeal_id = ? and user_id = ?
        LambdaQueryWrapper<ShoppingCart> qw =new LambdaQueryWrapper<>();
       // 获取userid
        Long aLong = UserHolder.get();
        //对比
        qw.eq(ShoppingCart::getUserId,aLong);
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //判断是否是菜品
        if (dishId!=null){
            //证明是菜品
            qw.eq(ShoppingCart::getDishId,dishId);
        }else {
            //证明是套餐
            qw.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(qw);
        Integer number = one.getNumber()-1;
        one.setNumber(number);
        if (number>0){
            shoppingCartService.updateById(one);
        }else{
            shoppingCartService.remove(qw);
        }
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R clean(){
        // 根据当前用户id进行删除：delete from shoping_cart where user_id = ?
        shoppingCartService.removeById(UserHolder.get());
        return R.success("清空购物车成功");
    }
}
